#include "videocapture.h"
#include <QDebug>


VideoCapture::VideoCapture(QObject *parent): QThread(parent)
{
    stopped=false;
    cam=NULL;
}

VideoCapture::~VideoCapture(){
    closeCamara();
}

void VideoCapture::run()
{
    while (!stopped)
        readFrame();
}

void VideoCapture::stop()
{
    stopped = true;
}

void VideoCapture::openCamara(){
    cam = cvCreateCameraCapture(0);
}

void VideoCapture::readFrame(){
    if(cam==NULL)
        return;
    frame = cvRetrieveFrame(cam,0);// 从摄像头中抓取并返回每一帧
    // 将抓取到的帧，转换为QImage格式。QImage::Format_RGB888不同的摄像头用不同的格式。
    QImage image = QImage((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888).rgbSwapped();
    emit renderedImage(image);
}

void VideoCapture::closeCamara(){
    if(cam!=NULL)
        cvReleaseCapture(&cam);
}

